<?php

use Illuminate\Database\Capsule\Manager as DB;

class FeedbackController extends CommonController
{
    public function typeAction()
    {
        $alltype = feedbackModel::getAllType();
        $type = [];
        if(!empty($alltype)){
            foreach ($alltype as $code => $name){
                array_push($type, ['code'=>$code, 'name'=>$name]);
            }
        }
        json(['code' => 1, 'msg' => '反馈类型', 'data' => $type]);
    }

    public function addAction()
    {
        try {
            if(!$this->isPost()){
                throw new Exception('非法请求');
            }
            $params = $this->postData;
            Validate::check($params, [
                'type' => ['role'=>'required', 'msg'=>'反馈类型'],
                'phone' => ['role'=>'required', 'msg'=>'联系方式'],
                'content' => ['role'=>'required', 'msg'=>'反馈内容'],
            ]);
            if(!is_numeric($params['content']) > 200){
                throw new Exception('反馈内容不能超过200个字');
            }
            $phone_validate = Validate::isNumber($params['phone']);
            if($phone_validate['code'] === 0){
                throw new Exception('联系方式只能是数字');
            }
            $params['images'] = implode(',', $params['images']);
            $this->cleanTableColumn((new feedbackModel())->getTable(), $params);
            $params['user_id'] = $this->user->id;
            $params['status'] = 0;
            $params['created_at'] = timestamp();
            $res = DB::transaction(function () use ($params) {
                return feedbackModel::query()->insert($params);
            });
            if($res){
                json(['code' => 1, 'msg' => '反馈成功，请等待客服处理！']);
            }else{
                throw new Exception('反馈失败');
            }
        }catch (Exception $e){
            json(['code' => 0, 'msg'=>$e->getMessage()]);
        }
    }

    public function listAction()
    {
        $query = feedbackModel::query()->where('user_id', $this->user->id);
        $mode = $this->get('mode', 'all');
        switch ($mode){
            case 'handled':
                $query->where('status', '=', 1);
                break;
            case 'unhandle':
                $query->where('status', '=', 0);
                break;
        }
        $total = $query->count();
        $query = $query->orderByDesc('created_at');
        $page = $this->get('page', 1);
        $limit = $this->get('rows', 10);
        $offset = ($page - 1) * $limit;
        $query = $query->offset($offset)->limit($limit);
        $rows = $query->get();
        json(['total' => $total, 'rows' => $rows->toArray()]);
    }

    public function detailAction()
    {
        try {
            $id = $this->get('id');
            $detail = feedbackModel::query()->where('user_id', $this->user->id)->where('id', $id)->first()->makeHidden('type', 'oper_id', 'deleted_at');
            if(!$detail){
                throw new Exception('反馈不存在');
            }
            json(['code' =>1, 'msg'=>'获取成功', 'data'=>$detail->toArray()]);
        }catch (Exception $e){
            json(['code'=>0, 'msg'=>$e->getMessage()]);
        }
    }
}
